﻿CREATE PROCEDURE [dbo].[InsertObjectData]
	@unique_num int out, 
	@class_num int,
	@major_obj int,
	@columns varchar(2000),
	@data varchar(2000)
AS
	declare @script varchar(4000)

	declare @table_name varchar(50);
	select @table_name = table_name from ClassesNames where class_num = @class_num;

	if @unique_num = -1 and @table_name = 'U_Object'
		begin
			DECLARE @TranName VARCHAR(20);
			set @TranName = 'InsertNewObject';

			BEGIN TRANSACTION @TranName;

			select @unique_num = (MAX(Unique_No) + 1) from U_Object;

			set @script = 'insert into ' + @table_name + '(Unique_No,Class,Major,Record_Date,' + @columns+ ')' +
							' values (' + convert(varchar,@unique_num)  + ',' + convert(varchar,@class_num) + ',' +
							convert(varchar,@major_obj) +',' + convert(varchar,@major_obj) + 
							',''' + convert(varchar,GETDATE()) + ''',' + @data + ');'
			exec @script
			COMMIT TRANSACTION @TranName;
		end
		else if @unique_num > 0
		begin
			set @script = 'insert into ' + @table_name + '(Unique_No,' + @columns+ ')' +
							' values (' + convert(varchar,@unique_num)  + ',' + @data + ');'
			exec @script
		end